package com.shuoyue.ycgk.ui.recruitment.his;

import android.os.Bundle;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.base.BaseMvpFragment;
import com.shuoyue.ycgk.base.ListWithPage;
import com.shuoyue.ycgk.base.baseadapter.AppBaseQuickAdapter;
import com.shuoyue.ycgk.entity.JobHis;
import com.shuoyue.ycgk.entity.JobInfo;
import com.shuoyue.ycgk.entity.RecruitmentItem;
import com.shuoyue.ycgk.entity.RecruitmentItemHis;
import com.shuoyue.ycgk.ui.recruitment.FocusNoticeListContact;
import com.shuoyue.ycgk.utils.SpacesItemDecoration;
import com.shuoyue.ycgk.views.dialog.common.HintConfirmDialog;

import butterknife.BindView;

public class FragmentHisNews
        extends BaseMvpFragment<FocusNoticeListContact.Presenter>
        implements FocusNoticeListContact.View {
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.refreshlayout)
    SmartRefreshLayout refreshlayout;

    HisNewsAdapter adapter;
    ListWithPage listWithPage;

    public static FragmentHisNews getInstance() {
        FragmentHisNews fragmentOrder = new FragmentHisNews();
        Bundle bundle = new Bundle();
//        bundle.putInt("type", type);
        fragmentOrder.setArguments(bundle);
        return fragmentOrder;

    }

    @Override
    protected void initData() {
        super.initData();
        mPresenter = new FocusNoticeListContact.Presenter();
        mPresenter.attachView(this);
        mPresenter.getHisRecruiment(1);
    }

    @Override
    public void initParams(Bundle bundle) {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_sigle_list;
    }

    @Override
    protected void initView() {
        adapter = new HisNewsAdapter(null);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.addItemDecoration(new SpacesItemDecoration(16, false));
        recyclerView.setAdapter(adapter);
        registEmptyView(adapter);
        refreshlayout.setEnableLoadMore(false);
        refreshlayout.setOnRefreshListener(refreshLayout -> {
            mPresenter.getHisRecruiment(1);
            refreshLayout.finishRefresh(2000);
        });
        refreshlayout.setOnLoadMoreListener(refreshLayout -> {
            mPresenter.getHisRecruiment(listWithPage.getCurrent() + 1);
            refreshLayout.finishLoadMore(2000);
        });
        registEmptyView(adapter);
        adapter.setDeleteCall((adapterTag, info) -> {
            HintConfirmDialog hintConfirmDialog = new HintConfirmDialog(mContext, "删除", "是否删除");
            hintConfirmDialog.setOkListener(v -> {
                mPresenter.deleteHis(adapterTag, info, info.getHistoryId());
                hintConfirmDialog.dismiss();
            });
            hintConfirmDialog.show();
        });
    }


    @Override
    public String getTitle() {
        return "招考公告";
    }

    @Override
    public void setFocuseNotice(ListWithPage<RecruitmentItem> list) {

    }

    @Override
    public void setFocuseJob(ListWithPage<JobInfo> list) {

    }

    @Override
    public void setHisNotice(ListWithPage<RecruitmentItemHis> list) {
        listWithPage = list;
        if (list.getCurrent() == 1) {
            adapter.resetData(list.getRecords());
        } else {
            adapter.addData(list.getRecords());
        }
        refreshlayout.setEnableLoadMore(list.getCurrent() != list.getPages() && list.getPages() > 0);
        refreshlayout.finishRefresh();
        refreshlayout.finishLoadMore();
    }

    @Override
    public void setHisJob(ListWithPage<JobHis> list) {

    }

    @Override
    public void deleteHis(AppBaseQuickAdapter adapter, Object o) {
        adapter.remove(adapter.getData().indexOf(o));
        adapter.notifyDataSetChanged();
    }
}
